using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Pisces
{
    /**
     * 无锁的单例，用于单线程
     */
    public abstract class LockSingleton<T> where T : class, InterfaceEntityLiftcycle
    {
        protected static T singleton;

        protected static readonly object objectLock = new object();
        
        public static T getSingleton()
        {
            lock (objectLock)
            {
                if (singleton == null)
                {
                    singleton = Activator.CreateInstance<T>();
                    singleton.onInit();
                }
                return singleton;    
            }
            
        }

        public static void destroy()
        {
            singleton = default(T);
            singleton.onDestroy();
        }
    }
}
